/* init.h
   By: Erin Szilagyi
   Date created: March 6th 2011
   Date modified: March 26th 2011
   Modified By: Samuel Ferguson
*/

#ifndef _XXXOS_INIT_H
#define _XXXOS_INIT_H

#include <stdint.h>
#include <interrupt.h>

#define IDT_ENTRIES 256
#define NUM_EXCEPTIONS 32

#define CLK_IDT_INDEX 0x50
#define KEYBOARD_IDT_INDEX 0x51

#define SYSCALL_IDT_INDEX 0x80

/*********************************/
/*** Typedefs ********************/
/*********************************/

/*********************************/
/*** Structs *********************/
/*********************************/

/* This structure holds an entry for the e820 */
struct e820_t
{
  unsigned int base_address_low;
  unsigned int base_address_high;
  unsigned int length_low;
  unsigned int length_high;
  unsigned int type; /* Type 1 -> AddressRangeMemory available */
} __attribute__((packed));

/* e820_raw is declared and initialized in kernel/head.S */
extern struct e820_t* e820_raw;
/* e820_counter is defined and initialized in kernel/head.S */
/* XXX why is this a pointer? */
extern uint16_t e820_counter;

/*Struct to hold the IDT Gate*/
struct idt_entry {
  uint16_t offset_l;
  uint16_t selector;
  uint8_t zero;
  uint8_t flags;
  uint16_t offset_h;
} __attribute__((packed));

/*Holds a pointer for idtr */
struct idt_ptr {
  uint16_t limit;
  uint32_t base;
} __attribute__((packed));

struct idt_entry idt[IDT_ENTRIES];
struct idt_ptr idtp;

/*********************************/
/*** Functions *******************/
/*********************************/

/* Function idt_load
 * void -> void
 * pre: none
 * post: The IDT is loaded with the table entries created in the function
 *       idt_set_gate
 * return : void
 */ 
extern void idt_load();

/* Function idt_set_gate
 * void -> void
 * pre: 
 * post: idt struct is filled with values so that idt_load can load
 *        them into memory
 * return: void
 */
void idt_set_gate();

/* Function idt_install
 * void -> void
 * pre: 
 * post: calls idt_set_gate, idt_load
 * return: void
 */
void idt_install();

/* Function set_exceptions
 * void -> void
 * pre:
 * post: sets up the exception vectors
 * return: void
 */
void set_exceptions();
#endif /* _XXXOS_INIT_H */
